package training.service;

import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import training.meta.UserAccountMeta;
import training.model.UserAccount;
import com.google.appengine.api.datastore.Transaction;


public class AccountService {

    // アカウント登録用
    public UserAccount regist(Map<String, Object> input) {
        UserAccount regist = new UserAccount();
        BeanUtil.copy(input, regist);
        Transaction tx = Datastore.beginTransaction();
        Datastore.put(regist);
        tx.commit();
        return regist;
    }
    private UserAccountMeta U = new UserAccountMeta();

    /*public List<UserAccount> getUserAccountList() {
        return Datastore.query(U).sort(U.userId.desc).asList();
    }*/
    /*//ユーザーID重複チェック
    public boolean duplicateCheck(List<UserAccount> AccountList, String userId) {
       for (UserAccount result : AccountList) {
           if (result.getUserId().equals(userId)) {
               return false;
           }
       }
       return true;
   }*/
    //ユーザーID重複チェック
    public boolean duplicateCheck2(String userId){
        List<UserAccount> list = Datastore.query(U).filter(U.userId.equal(userId)).sort(U.userId.asc).asList();
        if (list.size() == 0) {
            return true;
        }else{
            return false;
        }
    }
    /*//ログイン処理
    public boolean login(List<UserAccount> AccountList, String userId,String passWord) {
        for (UserAccount result : AccountList) {
            if (result.getUserId().equals(userId) && result.getPassWord().equals(passWord)) {
                return true;
            }
        }
        return false;
    }*/
    //ログイン処理
    public boolean login2(String userId,String passWord){
        UserAccount data = Datastore.query(U).filter(U.userId.equal(userId), U.passWord.equal(passWord)).asSingle();
        if (data == null) {
            return false;
        }
        return true;
    }
}
